﻿using System;
using System.Linq;
using BluePortal.QuikStart.LiveID;

namespace BluePortal.LiveID
{
    public class LiveIdService
    {
        BOWebDataContext _Context = null;
        public BOWebDataContext Context
        {
            get
            {
                if (_Context == null)
                {
                    _Context = new BOWebDataContext();
                    Context.Log = Console.Out;
                }
                return _Context;
            }
            set { _Context = value; }
        }

        public string FindLoginName(string liveidValue)
        {
            LiveId_LoginName l = Context.LiveId_LoginNames.SingleOrDefault(lid => lid.liveid == liveidValue);
            if (l != null)
                return l.loginName;
            else
                return string.Empty;
        }

        public void AddLoginName(string lidValue, string loginNameValue)
        {
            Context.LiveId_LoginNames.InsertOnSubmit(new LiveId_LoginName() { liveid = lidValue, loginName = loginNameValue });
            Context.SubmitChanges();
        }

        public void DeleteLoginName(string loginNameValue)
        {
            LiveId_LoginName l = Context.LiveId_LoginNames.SingleOrDefault(lid => lid.loginName == loginNameValue);
            if (l != null)
            {
                Context.LiveId_LoginNames.DeleteOnSubmit(l);
                Context.SubmitChanges();
            }
       }
    }
}
